package com.sgcc.fj.pmos.common.dialog;


import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.core.CenterPopupView;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.common.bean.UpdateInfoBean;
import com.sgcc.fj.pmos.common.bean.UpdateRootBean;
import com.sgcc.fj.pmos.common.http.DownloadUtil;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.widget.NumberProgressBar;

import java.io.File;

/**
 * @author ChaoAJ
 * @describe 版本更新弹窗
 * @time 2022/10/12 10:10
 */
public class UpdateAppDialog extends CenterPopupView {
    private UpdateInfoBean updateAppBean;
    private NumberProgressBar numProgressBar;
    private boolean isForce;//是否强制更新
    private LinearLayout llButtonLayout;
    private LinearLayout llProgress;


    public UpdateAppDialog(Context context, UpdateInfoBean updateEntity, boolean isForce) {
        super(context);
        this.updateAppBean = updateEntity;
        this.isForce = isForce;
    }


    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_update_app_layout;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        TextView tvVersion = findViewById(R.id.tvVersion);
        TextView tvUpdateInfo = findViewById(R.id.tvUpdateInfo);
        llButtonLayout = findViewById(R.id.llButtonLayout);
        llProgress = findViewById(R.id.llProgress);
        numProgressBar = findViewById(R.id.numProgressBar);
        TextView tvCancel = findViewById(R.id.tvCancel);
        tvCancel.setVisibility(isForce ? GONE : VISIBLE);
        tvCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                if (isForce)
                    System.exit(0);
            }
        });
        findViewById(R.id.tvUpdate).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(updateAppBean.getData().getAddress())) {
                    ToastUtils.showShort("暂无更新地址，更新失败");
                } else {
                    String filePath = getContext().getExternalFilesDir("apk") + "/" +
                            "ejy_" + updateAppBean.getData().getVersionName() + ".apk";
                    if (!TextUtils.isEmpty(filePath)) {//判断apk是否存在  存在直接安装不下载
                        boolean fileExists = FileUtils.isFileExists(filePath);
                        long fileLength = FileUtils.getFileLength(filePath);
                        AppUtils.AppInfo apkInfo = AppUtils.getApkInfo(filePath);
                        if (null != apkInfo) {
                            int appVersionCode = AppUtils.getAppVersionCode();
                            if (fileExists && fileLength > 10000 && apkInfo.getVersionCode() > appVersionCode) {
                                getContext().startActivity(IntentUtils.getInstallAppIntent(filePath));
                                return;
                            }
                        }
                    }


                    llButtonLayout.setVisibility(GONE);
                    llProgress.setVisibility(VISIBLE);
                    String url = updateAppBean.getData().getAddress().contains("http") ?
                            updateAppBean.getData().getAddress() :
                            HttpInterConst.BASE_URL1 + updateAppBean.getData().getAddress();
                    useApkDownLoadFunction(url, updateAppBean.getData().getVersionName());
                }
//                useApkDownLoadFunction("https://xuexiangjys.oss-cn-shanghai.aliyuncs.com/apk/xupdate_demo_1.0.2.apk");
            }
        });

        tvVersion.setText(updateAppBean.getData().getVersionName());
        tvUpdateInfo.setText(updateAppBean.getData().getContent());


    }

    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    private void useApkDownLoadFunction(String url, String versionName) {

        DownloadUtil.get().downloadApk(getContext(), url, versionName, new DownloadUtil.OnDownloadListener() {
            @Override
            public void onDownloadSuccess(File file) {
                llButtonLayout.setVisibility(VISIBLE);
                llProgress.setVisibility(GONE);
                getContext().startActivity(IntentUtils.getInstallAppIntent(file));
            }

            @Override
            public void onDownloading(int progress) {
                numProgressBar.setProgress(progress);
            }

            @Override
            public void onDownloadFailed(Exception e) {

            }
        });


    }

}